package com.bwie.redkids.fragment;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.bwie.redkids.DetailsActivity;
import com.bwie.redkids.R;
import com.bwie.redkids.adapter.SearchFragmentAdapter;
import com.bwie.redkids.bean.SearchFragemntBean;
import com.bwie.redkids.bean.SearchFragemntBean.SearchProdictList;
import com.bwie.redkids.utils.GsonUtils;
import com.bwie.redkids.utils.PathUtils;
import com.google.gson.Gson;

public class SearchFragment extends Fragment implements OnClickListener {

	protected static final int SUCESS = 0;
	private EditText et_search;
	private ImageView iv_search;
	private String trim;
	private ArrayList<SearchProdictList> arrayList = new ArrayList<SearchFragemntBean.SearchProdictList>();

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == SUCESS) {
				SearchFragemntBean searchFragemntBean = (SearchFragemntBean) msg.obj;
				arrayList.addAll(searchFragemntBean.getProductlist());
				search_list.setAdapter(new SearchFragmentAdapter(activity,
						arrayList));
			}
		};
	};
	private ListView search_list;
	private FragmentActivity activity;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.search_fragment, null, false);
		activity = getActivity();
		initView(view);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initData();
		search_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(getActivity(), DetailsActivity.class);
				intent.putExtra("id", arrayList.get(arg2).getId());
				startActivity(intent);
			}
		});
	}

	private void initView(View view) {
		et_search = (EditText) view.findViewById(R.id.et_search);
		iv_search = (ImageView) view.findViewById(R.id.iv_search);
		search_list = (ListView) view.findViewById(R.id.search_list);
		iv_search.setOnClickListener(this);
	}

	private void initData() {
		new Thread() {
			public void run() {
				try {
					String data = GsonUtils.getData(PathUtils.PATH
							+ PathUtils.SEARCH + "?keyword=" + trim);
					Gson gson = new Gson();
					SearchFragemntBean fromJson = gson.fromJson(data,
							SearchFragemntBean.class);
					handler.obtainMessage(SUCESS, fromJson).sendToTarget();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_search:
			trim = et_search.getText().toString().trim();
			initData();
			break;

		default:
			break;
		}
	}
}
